//app.js
const quote = require('utils/quoteConfig.js') //统一引入文件
const api = quote.api;
const Recorder = require('./vendor/skegn_weapp_sdk_v1/index.js').Recorder //引入外部录音接口
const WXlogin = quote.WXlogin;
const mta = require('utils/mta_analysis.js'); //引入腾讯埋点文件
App({
    onLaunch: function(res) {
       console.log(res,'3213131')
        var that = this
        new WXlogin(this, api); //微信登录获取用户openId
        //获取用户的设备信息
        mta.App.init({
            "appID": "500669041",
            "eventID": "500669042", // 高级功能-自定义事件统计ID，配置开通后在初始化处填写
            //"lauchOpts": options, //渠道分析,需在onLaunch方法传入options,如onLaunch:function(options){...}
            "statPullDownFresh": true, // 使用分析-下拉刷新次数/人数，必须先开通自定义事件，并配置了合法的eventID
            "statShareApp": true, // 使用分析-分享次数/人数，必须先开通自定义事件，并配置了合法的eventID
            "statReachBottom": true // 使用分析-页面触底次数/人数，必须先开通自定义事件，并配置了合法的eventID
        });
        wx.setInnerAudioOption({
            obeyMuteSwitch:false,
        })
    },

    login() { //登录获取openId和会话密钥
        let _this = this;
        return new Promise(function(resolve, reject) {
            wx.login({
                success: function(res) {
                    console.log('wx.login', res)
                    const code = res.code
                    if (code) {
                        let dataDic = {
                            appid: _this.globalData.appId,
                            secret: _this.globalData.appSecret,
                            js_code: code,
                        };
                        api.methodRequestWechatAPI({
                            dataDic,
                            success: (res) => {
                                const data = res.data;
                                if (data.code != 0) {
                                    console.log(res.data)
                                } else {
                                    console.log('获取用户openId成功', data.data)
                                    _this.globalData.openId = data.data.openid;
                                    _this.globalData.session_key = data.data.session_key;
                                    wx.setStorage({
                                        key: 'openid',
                                        data: data.data.openid,
                                        success: function(res) {
                                            console.log('openId写入缓存成功', res)
                                        }
                                    })
                                    wx.setStorage({
                                        key: 'session_key',
                                        data: data.data.session_key,
                                        success: function(res) {
                                            console.log('session_key写入缓存成功', res)
                                        }
                                    })
                                    var res = {
                                        status: 200,
                                        data: data.data.openid,
                                    }
                                    resolve(res);
                                }
                            },
                            fail: (res) => {
                                console.log('拉取用户openid失败，将无法正常使用开放接口等服务', res);
                            },
                            complete: (res) => {
                                console.log(res);
                            }
                        });
                    }
                }
            })
        })
    },
    globalData: {
        addId:0,
        system:null,//手机系统
        isBack: false, //判断是否从任务四结果页面返回任务四的标志
        isLongScrern: false, //长屏
        isBrang: false, //刘海屏
        scale: null,
        isPhoneX:false,
        moduleId:10,//模块ID
        courseName:'',//课程名字
        unitIndex: null, //单元数组下标
        taskStatus: null, //任务状态
        gameStatus: null, //游戏状态
        // taskStatus:{TaskOne:true,TaskTwo:true,TaskThree:false,TaskFour:true},//任务状态
        // gameStatus: { GameThree: true, GameOne: true, GameTwo: true},//游戏状态
        openId: null, //用户的openId
        userInfo: null, //用户信息
        appId: 'wx211bc96007658d1e', //小程序的appId
        appSecret: 'f226b6e242e7215190f6ac870fad20b3',
        // apiUrl: 'http://192.168.74.121:82',//本地接口地址
        // imgUrl: 'http://192.168.74.121/uploads/SmallProgram/BasicEnglishImg/Img/',//图片地址
        // audioUrl: 'http://192.168.74.121/uploads/SmallProgram/BasicEnglishAudio/Audio/',//录音地址
        imgUrl: 'https://video.acadsoc.com.cn//uploads/SmallProgram/BasicEnglishImg/Img/', //图片地址
        audioUrl: 'http://video.acadsoc.com.cn//uploads/SmallProgram/BasicEnglishAudio/Audio/', //录音地址
        apiUrl: "https://ies.acadsoc.com.cn",
        quote: quote,
        innerAudioContext: wx.createInnerAudioContext(), //创建一个音频播放器
        // BackgroundAudioManager:wx.getBackgroundAudioManager(),
        recorder: new Recorder('1510645856000042', '412ab8c468fcd728c584990850d3a77d'), //创建一个 Recorder 对象       
    }
})